// pages/activityList/activityList.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';

// let timerClear;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timerClear:null,
    tabList: ['正在拼团','限时秒杀'],
    tabIndex: 0, 
    page: 1, //分页
    size: 10, //分页的个数
    totalPages: 0, //总个数
    goodsList: [], //列表数据
    refreshFlag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    if (options.index != undefined){
      _this.setData({
        tabIndex: options.index
      })
    }
    _this.getSpellTypeData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      refreshFlag: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that=this;
    if (that.data.refreshFlag) {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      that.setData({
        refreshFlag: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timerClear);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉加刷新")
    let _this = this;
    if (!this.data.refreshFlag) {
      wx.showNavigationBarLoading();
    _this.setData({
      page: 1,
      goodsList: [],
    })
      wx.showNavigationBarLoading();
    _this.getSpellTypeData(function () {
      setTimeout(function () {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        _this.setData({
          refreshFlag: false
        })
      }, 1000)
    });
    }else{
      console.log('频繁刷新========')
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上拉加载")
    let _this = this;
    if (_this.data.page < _this.data.totalPages){
      _this.setData({
        page: _this.data.page + 1
      })
      wx.showLoading({ 
        title: '加载中',
      })
      _this.getSpellTypeData(function () {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },

  //tab切换
  tabChange(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    _this.setData({
      tabIndex: index,
      page:1,
      goodsList:[],
    })
    clearInterval(_this.data.timerClear);
    _this.getSpellTypeData();
  },

  //获取拼团和秒杀的列表数据
  getSpellTypeData(callback){
    //获取拼团信息	"spellType": "GROUP"
    //获取秒杀信息	"spellType":"LIMITED_TIME"
    let _this = this;
    let data = {
      "cityName": wx.getStorageSync('originalData').addressComponent.city,
      "spellType": _this.data.tabIndex == 1 ? "LIMITED_TIME" : "GROUP"
    }
    //console.log(data)
    Api.searchSpell(_this.data.page,_this.data.size,data).then( res => {
     // console.log(res)
      _this.setData({
        totalPages: res.data.totalPages,
        goodsList: _this.data.goodsList.concat(res.data.dataList),
      });
      if (_this.data.tabIndex == 1 && _this.data.goodsList.length > 0){
     var timerClear = setInterval(function () {
          _this.data.goodsList.forEach((item, index, arr) => {
            //console.log()
            if (util.getTimeLeft(item.endTime) < 0) {
              let dataTime = {
                days: "00",
                hours: "00",
                minutes: "00",
                seconds: "00"
              }
              arr[index].mssTime = dataTime;
              clearInterval(_this.data.timerClear);
              arr.splice(index, 1)
              _this.setData({
                goodsList: arr
              })
              _this.searchSpike();
              return;
            } else {
              arr[index].mssTime = util.getTimeLeft(item.endTime)
              _this.setData({
                goodsList: arr
              })
            }
          })
        }, 1000)
        _this.setData({
          timerClear:timerClear
        })
      } else {
        //console, log("没有列表数据")
      };
      (callback && typeof (callback) === "function") && callback();
    }).catch(err => {
      _this.setData({
        refreshFlag: false
      })
    });
  },

  //跳转拼团和秒杀的详情页 
  goDetailUrl(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    if(_this.data.tabIndex == 0){
      wx.navigateTo({
        url: `/pages/details/spellDetail?id=${_this.data.goodsList[index].id}`,
      })
    }else{
      wx.navigateTo({
        url: `/pages/details/spikeDetail?id=${_this.data.goodsList[index].id}`,
      })
    }
  }
})